home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / UNIX.C < prev    next >
C/C++ Source or Header  |  1993-06-11  |  5KB  |  234 lines

  1. #include "jam.h"
  2. #include "def.h"
  3. #include "stdio.h"
  4. #include "keyname.h"  
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8.  
  9. /* Stuff in DOS.C munged to work in BSD like UNIX world...
  10. * And other UNIX particular functions (but sadly, not all as xwin.c
  11. * has spawn commands!)
  12. */
  13.  
  14. /* Get directory listing from supplied path; this is bad
  15. * in that it uses ls vs internal scanning and is thus
  16. * quite stupid about directories, wildcards and the like.
  17. */
  18. void GetDiskDirectory(bp, inpath)
  19. BUFFER *bp;
  20. char *inpath;
  21. {
  22.   BOOL found = FALSE;
  23.   char buffer[512 + L_tmpnam + 1];
  24.   char out[L_tmpnam+1];
  25.   int r = FALSE;
  26.   register int i;
  27.   char shortpath[MAXPATH];
  28.   char path[MAXPATH];
  29.   char *wild = "*";
  30.  
  31.   WindowSleepCursor();
  32.  
  33.   /* find param, build cmd line
  34.   */
  35.   strcpy(path, inpath);
  36.   for (i = strlen(path) -1; i >= 0; i--)
  37.     if (path[i] == BDC1)
  38.       {
  39.         i++;
  40.         break;
  41.       }
  42.  
  43.   if (i >= 0)
  44.     {
  45.       strcpy(shortpath, &inpath[i]);
  46.       if (i > 0)
  47.         inpath[i - 1] = '\0';
  48.     }
  49.   else
  50.     shortpath[i] = '\0';
  51.   strcpy(path, inpath);
  52.  
  53. #if 0
  54.   for (i = 0; i < strlen(shortpath); i++)
  55.     if (shortpath[i] == '*')
  56.         {
  57.           found = TRUE;
  58.           break;
  59.         }
  60.   if (!found)
  61.     strcat(shortpath, wild);
  62. #endif
  63.  
  64.   tmpnam(out);
  65.   sprintf(buffer, "cd %s && ls -C %s > %s", path, shortpath, out);
  66.   r = system(buffer);
  67.   if (r == 127)
  68.     {
  69.       ewprintf("exec error");
  70.       ttbeep();
  71.       return;
  72.     }
  73.  
  74.   if (ffropen(out) == FIOSUC)
  75.     {
  76.       char line[NLINE];
  77.       int nbytes;
  78.  
  79.       while (ffgetline(line, NLINE, &nbytes) == FIOSUC)
  80.         {
  81.           line[nbytes] = '\0'; 
  82.           addline(bp, line);
  83.         }
  84.       ffclose();
  85.     }
  86.   else
  87.     ewprintf("I/O error creating list");
  88.  
  89.   /* cleanup
  90.   */
  91.   unlink(out);
  92.   WindowNormalCursor();
  93. }
  94. BOOL fileisrdonly(s)
  95. char *s;
  96. {
  97.   BOOL result = FALSE;
  98.   struct stat statbuf;
  99.   static int uid = -1, gid;
  100.  
  101.   if (uid == -1)
  102.     {
  103.       uid = getuid();
  104.       gid = getgid();
  105.       }
  106.  
  107.   if (!stat(s, &statbuf))
  108.     {
  109.       if (uid == statbuf.st_uid)  /* own files */
  110.     {
  111.       result = (statbuf.st_mode & S_IWRITE) == 0;
  112.     }
  113.       else if (gid = statbuf.st_gid)
  114.     {
  115.       result = (statbuf.st_mode & S_IWGRP) == 0;
  116.     }
  117.       else
  118.     {
  119.       result = (statbuf.st_mode & S_IWOTH) == 0;
  120.     }
  121.     }
  122.  
  123.   return (result);
  124. }
  125.  
  126. /* mail hackery
  127. */
  128. int sysmail(f, n)
  129. int f,n;
  130. {
  131.   char *mm;
  132.   static char mailer[L_tmpnam + 1] = {0};
  133.   static char user[NBUFN + 1] = {0};
  134.   char mailfilename[L_tmpnam + 2];
  135.   char    bufn[NBUFN], cmd[L_tmpnam * 3];
  136.   int s;
  137.   BUFFER *bp;
  138.  
  139.   if (!mailer[0])
  140.     {
  141.       mm = (char *)getenv("XNOTMAILER");
  142.       if (!mm || !(*mm))
  143.         {
  144.           epreload("mailx"); 
  145.           s = eread("Environment variable XNOTMAILER not set, use: ",
  146.                     mailer, L_tmpnam, EFNEW);
  147.           if (s != TRUE)
  148.             return(s);
  149.         }
  150.       else
  151.         strcpy(mailer, mm);
  152.     }
  153.  
  154.   tmpnam(mailfilename);
  155.   s = eread("Mail buffer: %s", bufn, NBUFN, EFNEW|EFBUF, curbp->b_bname);
  156.  
  157.   if (s == FALSE)
  158.     strcpy(bufn, curbp->b_bname);
  159.   else if (s == ABORT)
  160.     return (FALSE);
  161.  
  162.   if ((bp = bfind(bufn, FALSE)) == NULL) 
  163.     {
  164.       ttbeep();
  165.       ewprintf(nosuchbuffer);
  166.       return FALSE;
  167.     }
  168.  
  169.   epreload(user);
  170.   s = eread("To: ", user, NBUFN, EFNEW);
  171.   if (s != TRUE)
  172.     return(FALSE);
  173.  
  174.   /* Yes, I realize 'mailx' might not be everywhere....and that it
  175.   * is rude to have these system commands here as well as xwin.c, etc...
  176.   */
  177.   if (!writeout(bp, mailfilename))
  178.     {
  179.       ewprintf("Error writting file.");
  180.       return(FALSE);
  181.     }
  182.   ewprintf("Sending buffer...");
  183.   sprintf(cmd, "cat %s | %s %s", mailfilename, mailer, user);
  184.   system(cmd);
  185.   unlink(mailfilename); 
  186.   ewprintf("Buffer '%s' sent to '%s'", bufn, user);
  187.   return (TRUE);
  188. }
  189.  
  190. void anynewmail()
  191. {
  192.   static char *mailpath = NULL;
  193.   static char mailfile[NFILEN] = {0};
  194.  
  195.   if (!mailfile[0])
  196.     {
  197.       mailpath = (char *)getenv("MAIL");
  198.  
  199.       if (mailpath && *mailpath)
  200.         strcpy(mailfile, mailpath);
  201.       else
  202.         {
  203.           strcpy(mailfile, "/usr/mail/");
  204.           strcat(mailfile, getenv("USER"));
  205.         }
  206.     }
  207.  
  208.   if (mailfile[0])
  209.     {
  210.       struct stat  statbuf;
  211.       static time_t lasttime = 0;
  212.       static off_t lastsize = -1;
  213.       BOOL oldsomemail = somemail;
  214.  
  215.       if (!stat(mailfile, &statbuf))
  216.         {
  217.           if ((lasttime =! 0) && (difftime(lasttime, statbuf.st_mtime)))
  218.             {
  219.               if ((statbuf.st_size != 0) && (lastsize != statbuf.st_size))
  220.                 if (!oldsomemail)
  221.                   {
  222.                     ttbeep();
  223.                     ttbeep();
  224.                   }
  225.             }
  226.           lasttime = statbuf.st_mtime;
  227.           lastsize = statbuf.st_size;
  228.           somemail = (statbuf.st_size == 0) ? FALSE : TRUE;
  229.           if (oldsomemail != somemail)
  230.             if (!eprompting)
  231.               noop();    /* force update */
  232.         }
  233.     }
  234. }